package app.fugema.shop.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import app.fugema.shop.R;

public class PickerDialog extends Dialog implements View.OnClickListener
{
    private Context mContext;

    private TextView mSelectTitle;
    private View lineTag;
    private ListView listview;
    private Button mBtnCancel;
    // 标题
    private String mTitle = "";
    // 选择的 内容
    private String[]  contentStr = new String[]{};
    //ArrayAdapter<String>
    private ListStringAdapter arrayAdapter;

    public PickerDialog(Context context , OnItemClickListener listener)
    {
        super(context , R.style.time_dialog );
        this.mContext = context;
        setOnItemClickListener(listener);
        initDialog();
    }

    public PickerDialog(Context context, int theme , OnItemClickListener listener)
    {
        super(context, theme);
        this.mContext = context;
        setOnItemClickListener(listener);
        initDialog();
    }

    public PickerDialog(Context context, String title , OnItemClickListener listener)
    {
        super(context ,R.style.time_dialog);
        this.mContext = context;
        this.mTitle = title;
        setOnItemClickListener(listener);
        initDialog();
    }

    public PickerDialog(Context context , int theme , String title, OnItemClickListener listener)
    {
        super(context, theme);
        this.mContext = context;
        this.mTitle = title;
        setOnItemClickListener(listener);
        initDialog();
    }

    /**
     * 初始化Dialog
     */
    public void initDialog()
    {
        Window win = getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        win.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        setCanceledOnTouchOutside(false);
    }

    /**
     * 隐藏Dialog
     */
    private void hideDialog()
    {
        //hide();
        cancel();
        dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_content_picker);

        initView();
        initListener();
    }

    private void initView()
    {
        mSelectTitle = (TextView) findViewById(R.id.tv_select_title);
        lineTag = (View) findViewById(R.id.line_tag);
        listview = (ListView) findViewById(R.id.listview);
        mBtnCancel = (Button) findViewById(R.id.btn_cancel);
        //设置 标题
        if(!TextUtils.isEmpty(mTitle))
        {
            mSelectTitle.setText( mTitle );
        }
        arrayAdapter = new ListStringAdapter(mContext,contentStr);
        // ArrayAdapter<String>(mContext, R.layout.list_text_item, contentStr);
        listview.setAdapter( arrayAdapter );
    }

    private void initListener()
    {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                mListener.onItemClick(  arrayAdapter.getItem(position),position  );
                hideDialog();
            }
        });

        mBtnCancel.setOnClickListener(this);

        setOnKeyListener(new DialogInterface.OnKeyListener()
        {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
            {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {  //返回键监听
                    //返回键关闭dialog
                    hideDialog();
                }
                return false;
            }
        });
    }

    /**
     * 显示 选择内容  的弹出框
     * @param contents   选择内容
     */
    public void showPickContent(String[] contents)
    {
        this.show();
        mSelectTitle.setText( mContext.getString(R.string.select_title) );
        this.contentStr = null;
        this.contentStr = contents;
        this.arrayAdapter.setData(this.contentStr);

    }

    /**
     * 显示 选择内容  的弹出框
     * @param contents   选择内容
     */
    public void showPickContentNoTitle(String[] contents)
    {
        this.show();
        mSelectTitle.setVisibility(View.GONE);
        lineTag.setVisibility(View.GONE);
        this.contentStr = null;
        this.contentStr = contents;
        this.arrayAdapter.setData(this.contentStr);
    }

    /**
     * 显示 选择内容  的弹出框
     * @param title  标题
     * @param contents  选择内容
     */
    public void showPickContent(String title , String[] contents)
    {
        this.show();
        this.mTitle = title;
        //设置 标题
        if(!TextUtils.isEmpty(mTitle))
        {
            mSelectTitle.setText( mTitle );
        }
        this.contentStr = null;
        this.contentStr = contents;
        this.arrayAdapter.setData(this.contentStr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (isOutOfBounds(getContext(), event))
        {
            //触摸dialog外部关闭dialog
            hideDialog();
            // 选择图片时  关闭dialog 时 关闭activity
            if( mCancelListener != null)
            {
                mCancelListener.onCancelClick();
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 判断当前用户触摸是否超出了Dialog的显示区域
     *
     * @param context
     * @param event
     * @return
     */
    private boolean isOutOfBounds(Context context, MotionEvent event)
    {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    public void onClick(View v)
    {

        if (v.getId() == R.id.btn_cancel)
        {
            //点击“取消”关闭dialog
            hideDialog();
            if( mCancelListener != null)
            {
                mCancelListener.onCancelClick();
            }
        }
    }

    OnItemClickListener mListener;

    OnCancelClickListener mCancelListener;

    public interface OnItemClickListener
    {
        /**
         * 点击条目
         *
         * @param selectStr 点击条目
         */
        void onItemClick(String selectStr, int position);
    }

    public interface OnCancelClickListener
    {
        void onCancelClick();
    }

    public void setOnItemClickListener(OnItemClickListener listener)
    {
        this.mListener = listener;
    }

    public void setOnCancelListener(OnCancelClickListener cancelListener)
    {
        this.mCancelListener = cancelListener;
    }
}
